package com.coreJava.renwubianpai;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 作者: LCG
 * 日期: 2020/2/9 17:42
 * 描述: 测试jdk1.8的新特性任务的编排整理
 */
public class TestRenWu {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //线程2依赖于线程1的执行结果
        /*CompletableFuture.supplyAsync(()-> {
            int i=0;
            i++;
            return i;
        })
        .thenAccept(x->{
           System.out.println(x+"---------->");
        });*/

        //任务的合并 线程三依赖线程一和二的结果
       /* CompletableFuture.supplyAsync(()->{
            return "第一个线程的结果";
        }).thenCombineAsync(//合并两个线程的结果的方法
                CompletableFuture.supplyAsync(()->{return "第二个结果";}),(x,y)->{return x+y;}
                ).thenAccept(x->{
            System.out.println(x+"------线程三的结果");
        });*/

        //所有的线程中有一个先执行完的
        /*CompletableFuture<String> st1 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "T=01";
        });
        CompletableFuture<String> st2 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(6000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "T=02";
        });

        CompletableFuture<String> st3 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(8000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "T=03";
        });*/

        //会阻塞当前程序
       /* CompletableFuture<Object> objectCompletableFuture = CompletableFuture.anyOf(st1, st2, st3);
        System.out.println(objectCompletableFuture.get());*/

        //会阻塞主程序
        /*CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(st1, st2, st3);
        System.out.println(voidCompletableFuture.get());
        System.out.println("==================");*/
        //

        //创建一个没有返回值的线程
        /*CompletableFuture.runAsync(() -> {
            System.out.println("====");
        });
        System.out.println("======88======");*/

        //创建一个有返回值的线程 这种的会阻塞主线程
        /*CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            return "ok";
        });
        System.out.println(stringCompletableFuture.get());*/

        //异常的处理
        String str="00";
        CompletableFuture<String> exceptionally = CompletableFuture.supplyAsync(() -> {
            if (str.equals("00")) {
                throw new RuntimeException("=======错误提示=======");
            }
            return test(str);
        }).thenCombineAsync(CompletableFuture.supplyAsync(() -> {
            return test(str);
        }), (x, y) -> x + y)
                .exceptionally((e) -> {
                    e.printStackTrace();
                    return "error";
                });
        System.out.println(exceptionally.get());

    }

    /**
     * 测试方法
     * @param parm
     * @return
     */
    public static String test(String parm){

        return parm+"---test";
    }

}